<template>
  <div class="product-detail" v-if="product">
    <div class="container">
      <h1 class="page-title">产品详情</h1>
      
      <div class="product-content">
        <!-- 左侧图片 -->
        <div class="product-image">
          <img 
            :src="product.images[0] || '/placeholder.jpg'" 
            :alt="product.name_en"
            @error="handleImageError"
          />
        </div>
        
        <!-- 右侧信息 -->
        <div class="product-info">
          <h2 class="product-name">{{ product.name_en }}</h2>
          <div class="publish-time">Publish time: {{ formatDate() }}</div>
          
          <div class="product-details">
            <p>{{ product.description_en }}</p>
            <div v-if="product.specifications_en" class="specifications">
              <p>{{ product.specifications_en }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 产品未找到 -->
  <div v-else-if="!loading" class="product-not-found">
    <div class="container">
      <p>Product not found</p>
      <router-link to="/products">Back to Products</router-link>
    </div>
  </div>

  <!-- 加载状态 -->
  <div v-if="loading" class="loading">
    <div class="container">
      <p>Loading...</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, computed, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useProductsStore } from '../stores/products'

const route = useRoute()
const productsStore = useProductsStore()
const loading = ref(true)

const productId = computed(() => parseInt(route.params.id as string))
const product = computed(() => {
  return productsStore.getProductById(productId.value)
})

const formatDate = () => {
  return '2023-06-15 23:35'
}

const handleImageError = (event: Event) => {
  const img = event.target as HTMLImageElement
  img.src = '/placeholder.jpg'
}

watch(() => route.params.id, async (newId) => {
  if (newId) {
    loading.value = true
    if (!productsStore.settings.products_list) {
      await productsStore.fetchSettings()
    }
    loading.value = false
  }
}, { immediate: true })

onMounted(async () => {
  loading.value = true
  await productsStore.fetchSettings()
  loading.value = false
})
</script>

<style scoped>
.product-detail {
  min-height: 100vh;
  background: #ffffff;
  padding: 40px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-title {
  font-size: 18px;
  font-weight: normal;
  margin-bottom: 30px;
  color: #333;
}

.product-content {
  display: flex;
  gap: 40px;
  align-items: flex-start;
}

.product-image {
  flex: 0 0 400px;
  max-width: 400px;
}

.product-image img {
  width: 100%;
  height: auto;
  border: 1px solid #e0e0e0;
}

.product-info {
  flex: 1;
}

.product-name {
  font-size: 24px;
  font-weight: normal;
  margin-bottom: 10px;
  color: #000;
  text-transform: uppercase;
}

.publish-time {
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

.product-details {
  font-size: 14px;
  line-height: 1.8;
  color: #333;
}

.product-details p {
  margin-bottom: 15px;
}

.specifications {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e0e0e0;
}

.specifications p {
  white-space: pre-line;
}

.product-not-found {
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-not-found p {
  margin-bottom: 20px;
  color: #666;
}

.product-not-found a {
  color: #007bbf;
  text-decoration: none;
}

.product-not-found a:hover {
  text-decoration: underline;
}

.loading {
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}

@media (max-width: 768px) {
  .product-content {
    flex-direction: column;
  }
  
  .product-image {
    flex: none;
    max-width: 100%;
  }
}
</style>